using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UpgradeChargeShot:Upgrade {

	float timer;
	GameObject effect;
	SpriteRenderer effectSprite;

	float charge => (timer<2) ? timer*0.5f : Mathf.Clamp01(6-timer);

	public override void Update(object sender,WeaponEventArgs e) {
		base.Update(sender,e);
		if(!effect) {
			effect=ParticleManager.Create("AmpGlow",weapon.muzzle.position);
			effectSprite=effect.GetComponent<SpriteRenderer>();
			effect.transform.parent=weapon.muzzle;
		}

		Color color = new Color(1,.7687f,.3349f,charge);
		effectSprite.color=color;
		effect.transform.rotation=new Angle(45).quaternion;
		//effect.transform.position=weapon.muzzle.position;

		timer+=deltaTime;

		if(charge!=0) AddProgression(charge);

	}

	public override void OnDisable() {
		effect?.SetActive(false);
		timer=0;
		effect=null;
	}

	public override void OnDequip() {
		base.OnDequip();
		effect?.SetActive(false);
		effect=null;
	}

	float chargeConsumed;

	protected override void ShootingEventHandler(object _sender,ShootEventArgs e) {
		base.ShootingEventHandler(_sender,e);
		if(!IfReactWeaponEvent(_sender,e)) return;
		e.stat.recoil.AddAdd(charge*2,0);
		chargeConsumed=charge;
		timer=0;
	}

	protected override void ProjectileFiringEventHandler(object _sender,ProjectileEventArgs e) {
		base.ProjectileFiringEventHandler(_sender,e);
		if(!IfReactProjectileEvent(_sender,e)) return;
		e.stat.damage.AddMultAdd(chargeConsumed*0.5f,0);
		e.stat.knockback.AddAdd(chargeConsumed,0);
		e.stat.size.AddMultAdd(chargeConsumed*0.4f,0);

		if(chargeConsumed>=1) {
			Projectile sender = _sender as Projectile;
			sender.stat.damagePrototype.crit=true;
			sender.AddModifier(new ModifierElectrified(sender,null));
		}
	}

}